package jee.boot.common.utils.excel.service;

import com.google.common.collect.Lists;
import jee.boot.common.cache.CacheDataManager;
import jee.boot.common.utils.SpringContextHolder;
import jee.boot.common.utils.StringUtils;
import jee.boot.entity.system.Dictionary;
import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典类型工具类,使用时候先new出来吧,用完之后记得clear()下
 */
public class DictionaryTranslateHelper {
    private static final Logger logger = LoggerFactory.getLogger(DictionaryTranslateHelper.class);
    private Map<String, List<Dictionary>> dictMap;

    private DictionaryTranslateHelper() {
        this.dictMap = new HashMap<>();
        initDictionary();
    }

    public static DictionaryTranslateHelper getInstance() {
        return new DictionaryTranslateHelper();
    }

    public String getDictLabel(String value, String type) {
        return getDictLabel(value, type, "i18n.label");
    }

    public String getDictLabel(String value, String type, String field) {
        if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)) {
            List<Dictionary> dictList = getDictList(type);
            for (Dictionary dict : dictList) {
                if (type.equals(dict.getType()) && value.equals(dict.getValue())) {
                    return getFieldValue(dict, field);
                }
            }
        }
        return null;
    }

    public String getDictLabels(String values, String type) {
        return getDictLabels(values, type, "i18n.label");
    }

    public String getDictLabels(String values, String type, String field) {
        if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(values)) {
            List<String> valueList = Lists.newArrayList();
            for (String value : StringUtils.split(values, ",")) {
                valueList.add(getDictLabel(value, type, field));
            }
            return StringUtils.join(valueList, ",");
        }
        return null;
    }

    public String getDictValue(String label, String type) {
        return getDictValue(label, type, "i18n.label");
    }

    public String getDictValue(String label, String type, String field) {
        if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(label)) {
            List<Dictionary> dictList = getDictList(type);
            for (Dictionary dict : dictList) {
                if (type.equals(dict.getType()) && label.equals(getFieldValue(dict, field))) {
                    return dict.getValue();
                }
            }
        }
        return null;
    }

    public String getDictValues(String labels, String type, String field) {
        if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(labels)) {
            List<String> valueList = Lists.newArrayList();
            for (String label : StringUtils.split(labels, ",")) {
                valueList.add(getDictValue(label, type, field));
            }
            return StringUtils.join(valueList, ",");
        }
        return null;
    }

    /**
     * 根据类型获取字典列表
     *
     * @param type
     * @return
     */
    public List<Dictionary> getDictList(String type) {
        return dictMap.get(type);
    }

    private void initDictionary() {
        String cacheKey = new Dictionary().getCacheKey();
        try {
            List<Dictionary> list = getCacheDataManager().getCacheData(cacheKey);
            for (Dictionary dict : list) {
                List<Dictionary> dictList = dictMap.get(dict.getType());
                if (dictList == null) {
                    dictList = new ArrayList<>();
                    dictMap.put(dict.getType(), dictList);
                }
                dictList.add(dict);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

    }

    private String getFieldValue(Object object, String field) {
        try {
            return (String) PropertyUtils.getProperty(object, field);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 清理缓存使用
     */
    public void clear() {
        dictMap.clear();
    }

    public CacheDataManager getCacheDataManager() {
        return SpringContextHolder.getBean(CacheDataManager.class);
    }

}
